package org.softmed.rest.editor;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Method;
import java.net.URI;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import org.softmed.rest.admin.file.FileBrowser;
import org.softmed.rest.admin.file.FileManagerProxy;
import org.softmed.rest.editor.commons.AddressBar;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.rest.editor.comps.ApplicationEditor;
import org.softmed.rest.editor.comps.HandlerEditor;
import org.softmed.rest.editor.comps.HttpMethodEditor;
import org.softmed.rest.editor.comps.MimeTypeHandlerEditor;
import org.softmed.rest.editor.comps.ModuleEditor;
import org.softmed.rest.editor.comps.ParameterEditor;
import org.softmed.rest.editor.comps.ResourceEditor;
import org.softmed.rest.editor.comps.SimpleEditor;
import org.softmed.rest.editor.comps.browser.PagedListPanel;
import org.softmed.rest.editor.comps.browser.RESTServiceBrowser;
import org.softmed.rest.editor.comps.browser.SelectionListener;
import org.softmed.rest.editor.comps.utils.list.showListFieldDetails;
import org.softmed.rest.editor.tabs.CloseableTabbedPane;
import org.softmed.swing.IconManager;

public class EditorPanel extends JFrame implements URIProvider,
		SelectionListener, FilePathProvider, KeyListener, ActionListener {

	JPanel top = new JPanel();

	AddressBar addressBar = new AddressBar();
	String restAdminURI = "admin/rest";
	CloseableTabbedPane mainZone = new CloseableTabbedPane();
	RESTServiceBrowser restBrowser = new RESTServiceBrowser(addressBar);
	FileBrowser fileBrowser = new FileBrowser("rest-editor-root-directory.txt");

	ComplexEditorDecider decider = new ComplexEditorDecider(restBrowser);

	JButton open = new JButton("Get Server Applications",
			IconManager.updateToServer);

	// ControlPane controlPane = new ControlPane();

	JSplitPane splitRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	JSplitPane splitLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

	public EditorPanel() throws Throwable {

		top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
		top.add(addressBar);
		top.add(open);
		open.addActionListener(this);
		SwingUtil.setSize(open, 170, 20);
		open.setToolTipText("Show server applications");
		top.add(Box.createHorizontalStrut(5));
		top.add(new AboutButton());

		// about.addActionListener(this);

		addressBar.getUriField().addKeyListener(this);
		// controlPane.setUriProvider(addressBar);

		// SwingUtil.setSize(this, 1280, 768);
		setPreferredSize(new Dimension(1280, 768));
		// setResizable(false);
		setTitle("REST Service Editor - alpha version 0.50.00");
		setLayout(new BorderLayout());

		restBrowser.setSelectionListener(this);
		fileBrowser.setFileManager(new FileManagerProxy());

		add(top, BorderLayout.NORTH);
		// add(mainZone, BorderLayout.CENTER);
		// add(new JScrollPane(restBrowser), BorderLayout.EAST);
		// add(new JScrollPane(fileBrowser), BorderLayout.WEST);

		JScrollPane centerScroll = new JScrollPane(mainZone);
		centerScroll.setMinimumSize(new Dimension(685, 700));
		// mainZone.setMinimumSize(new Dimension(687, 700));
		// mainZone.setMaximumSize(new Dimension(685, 700));
		splitRight.add(mainZone);

		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
		panel.add(Box.createHorizontalStrut(2));
		panel.add(restBrowser);

		JScrollPane rightScroll = new JScrollPane(panel);
		// rightScroll.setMinimumSize(new Dimension(337, 700));

		splitRight.add(rightScroll);

		splitRight.setDividerLocation(685);
		splitRight.setResizeWeight(0.0);

		JScrollPane leftScroll = new JScrollPane(fileBrowser);
		// leftScroll.setMinimumSize(new Dimension(245, 700));

		splitLeft.add(leftScroll);
		splitLeft.add(splitRight);

		splitLeft.setDividerLocation(245);

		splitLeft.setOneTouchExpandable(true);
		splitRight.setOneTouchExpandable(true);

		add(splitLeft, BorderLayout.CENTER);

		pack();
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				try {
					addressBar.writeLastURI();
					fileBrowser.saveRootDirectory();
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.exit(0);

			}
		});

		setVisible(true);
		setExtendedState(JFrame.MAXIMIZED_BOTH);

		showServerApps();

	}

	@Override
	public String getURI() {
		if (addressBar.getURI().endsWith("/"))
			return addressBar.getURI() + restAdminURI;
		else
			return addressBar.getURI() + "/" + restAdminURI;
	}

	@Override
	public void selected(Object object) {
		System.out.println("Selected : " + object);

		SimpleEditor editor = null;
		try {
			editor = decider.getEditor(object);

			String name = object.getClass().getSimpleName();
			name = name.substring(0, 4) + " - ";

			Method getId = object.getClass().getMethod("getId");
			Long id = (Long) getId.invoke(object, null);
			name += id;

			if (editor instanceof showListFieldDetails) {
				showListFieldDetails temp = (showListFieldDetails) editor;
				temp.showListFieldDetails();
			}

			if (editor instanceof FilePathAware) {
				FilePathAware temp = (FilePathAware) editor;
				temp.setFilePathProvider(this);
			}

			editor.maximize();
			mainZone.addCloseableTab(name, editor);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public String getSelectedFilePath() {
		return fileBrowser.getCurrentSelectedNodePath();
	}

	@Override
	public String getSelectedFileRelativePath() {
		return fileBrowser.getCurrentSelectedNodeRelativePath();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER)
			showServerApps();

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		showServerApps();

	}

	private void showServerApps() {
		try {
			restBrowser.showApps();
		} catch (Throwable e1) {
			e1.printStackTrace();
			EditorUtil.hideUpdatePanel();
			JOptionPane.showMessageDialog(this, e1.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

}
